package com.digitalchina.lxnsy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.PermissionListVO;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.adapter
 * 创建人：郑皓
 * 创建时间：2019/4/23 18:15
 * 修改备注：
 * ================================================
 */
public class SetPermissionAdapter extends BaseExpandableListAdapter {

    private List<PermissionListVO> list = new ArrayList<>();


    public void setData(List<PermissionListVO> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public List<PermissionListVO> getList() {
        return this.list;
    }


    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return list.get(i).getChildren().size();
    }

    @Override
    public PermissionListVO getGroup(int i) {
        return list.get(i);
    }

    @Override
    public PermissionListVO getChild(int i, int j) {
        return list.get(i).getChildren().get(j);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int j) {
        return j;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        GroupViewHolder groupViewHolder;
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_permission_ex_partent, viewGroup, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvName = view.findViewById(R.id.tv_name);
            view.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        groupViewHolder.tvName.setText(list.get(i).getName());
        return view;
    }

    @Override
    public View getChildView(final int i, final int j, final boolean b, View view, ViewGroup viewGroup) {
        final ChildViewHolder childViewHolder;
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_permission_ex_child, viewGroup, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.checkBox = view.findViewById(R.id.checkbox);
            view.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) view.getTag();
        }
        childViewHolder.checkBox.setText(list.get(i).getChildren().get(j).getName());

        if (list.get(i).getChildren().get(j).getCheckStatus() == 1)
            childViewHolder.checkBox.setChecked(true);
        else
            childViewHolder.checkBox.setChecked(false);

        childViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (childViewHolder.checkBox.isChecked())
                    list.get(i).getChildren().get(j).setCheckStatus(1);
                else
                    list.get(i).getChildren().get(j).setCheckStatus(0);
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int j) {
        return false;
    }

    static class GroupViewHolder {
        TextView tvName;
    }

    static class ChildViewHolder {
        CheckBox checkBox;
    }
}
